<?php

namespace App\Web\Controller;

use App\Web\Facades\UserAuth;
use App\Web\Model\OperatorLog;
use EchoPHP\Facades\Db;
use EchoPHP\Foundation\Exception\HttpException;
use EchoPHP\Http\Request;
use App\Web\Model\Task as TaskModel;
use App\Web\Model\TaskDetail as TaskDetailModel;


class Task extends Controller {

    /**
     * 查询我发起的任务列表
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function select(Request $request) {
        $loginId = $this->getLoginId();
        $pageNum = $request->getParam('pagenum/d');
        $pageLimit = $request->getParam('pagelimit/d');
        $search = [
            'taskname' => $request->getParam('taskname'),
            'tasktype' => $request->getParam('tasktype'),
            'startdate' => $request->getParam('startdate'),
            'enddate' => $request->getParam('enddate'),
            'searchreceiver' => $request->getParam('searchreceiver'),
            'status' => $request->getParam('status')
        ];
        $searchtype = $request->getParam('searchtype');

        $isTeamLeader = UserAuth::ckhTeamLeader();

        if ($searchtype == 'send') {//查询我发起的任务列表
            $taskModel = new TaskModel();
            list($pageNum, $rowFrom, $rowTo) = $taskModel->paginationParameters($pageNum, $pageLimit);
            $count = $taskModel->getListCount($loginId, $search);
            if (0 == $count) {
                return $this->json(['status' => true, 'message' => '获取成功', 'count' => 0, 'tasklist' => [],'isteamleader' => $isTeamLeader]);
            }
            $taskList = $taskModel->selinfo($loginId, $search, '', $rowFrom, $rowTo);
        } elseif ($searchtype == 'receive' || $searchtype = 'team') {//我收到的任务列表
            $taskDetailModel = new TaskDetailModel();
            list($pageNum, $rowFrom, $rowTo) = $taskDetailModel->paginationParameters($pageNum, $pageLimit);
            $isTeam = $searchtype == 'team';
            $count = $taskDetailModel->getListCount($loginId, $search,$isTeam);
            if (0 == $count) {
                return $this->json(['status' => true, 'message' => '获取成功', 'count' => 0, 'tasklist' => [],'isteamleader' => $isTeamLeader]);
            }
            $taskList = $taskDetailModel->selinfo($loginId, $search, '', $rowFrom, $rowTo,'', $isTeam);
        } else {
            return $this->json(['status' => true, 'message' => '获取成功', 'count' => 0, 'tasklist' => [],'isteamleader' => $isTeamLeader]);
        }

        $return = [
            'status' => true,
            'message' => '获取成功',
            'tasklist' => $taskList,
            'isteamleader' => $isTeamLeader
        ];
        if (isset($count)) {
            $return['count'] = $count;
        }
        return $this->json($return);
    }

    /**
     * 附件下载
     * @param Request $request
     * @return \EchoPHP\Http\Response\BinaryFileResponse
     * @throws \Exception
     */
    public function downloadAttach(Request $request) {
        $taskId = $request->getParam('taskid/d');
        $taskModel = new TaskModel();
        $taskInfo = $taskModel->getTaskAttach($taskId);
        if (empty($taskInfo)) {
            throw new HttpException(500, '附件不存在');
        }
        $filePath = FILESERVER . '/' . $taskInfo['attachpath'];
        return response()->download($filePath, $taskInfo['attachname']);
    }

    /**
     * 新增任务
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function add(Request $request) {
        $loginId = $this->getLoginId();
        //获取提交参数
        $params = array();
        $params['taskname'] = $request->getPost('taskname');
        $params['tasktype'] = $request->getPost('tasktype');
        $params['startdate'] = $request->getPost('startdate');
        $params['enddate'] = $request->getPost('enddate');
        $params['memo'] = $request->getPost('memo');
        $params['isfullday'] = $request->getPost('isfullday/d', 0);
        $params['resumeid'] = $request->getPost('resumeid');
        $params['projectname'] = $request->getPost('projectname');
        $params['projectid'] = $request->getPost('projectid');
        $params['submittername'] = $request->getPost('submittername');
        $params['receiverinfo'] = $request->getPost('receiverinfo');
        $params['receiverinfo'] = json_decode($params['receiverinfo'], true);
        $attach = $request->getFile('attach');

        //执行
        $taskModel = new TaskModel();
        $executeResult = $taskModel->saveTaskInfo($params, $attach);
        return $this->json($executeResult);
    }

    /**
     * 完成任务
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function finishTask(Request $request) {
        $oprId = $this->getLoginId();
        $taskDetailID = $request->getPost('taskdetailid/d');
        $handleType = $request->getPost('handletype', 'finish');  //finish-设置为完成  unfinish-设置为未完成
        $taskModel = new TaskModel();
        $result = $taskModel->finishInfo($oprId, $taskDetailID, $handleType);
        if ($result === false) {
            return $this->json(['status' => false, 'message' => '操作失败']);
        }
        return $this->json(['status' => true, 'message' => '操作成功']);
    }

    /**
     * 任务详情页
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getTaskInfo(Request $request) {
        $oprId = $this->getLoginId();
        $taskID = $request->getPost('taskid/d');
        $taskModel = new TaskModel();
        $result = $taskModel->getTaskInfo($oprId, $taskID);
        $result = [
            'status' => empty($result) ? false : true,
            'message' => empty($result) ? '任务信息有误' : '获取成功',
            'taskinfo' => $result,
        ];
        return $this->json($result);
    }

    /**
     * 未完成任务数
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getUnfinishedCount(){
        $oprId = $this->getLoginId();
        $taskModel = new TaskModel();
        $result = $taskModel->getUnfinishedCount($oprId);
        return $this->json(['status'=>true,'message'=>'获取成功','count'=>$result]);
    }

    /**结束任务 需是任务创建人
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function endTask(Request $request){
        $oprId = $this->getLoginId();
        $taskId = $request->getPost('taskid'); //多个任务逗号隔开
        $taskModel = new TaskModel();
        $result = $taskModel->endTask($oprId,$taskId);
        return $this->json($result);
    }
}
